﻿using huqiang.Core.HGUI;
using huqiang.Data;
using huqiang.UIEvent;
using System;
using System.Collections.Generic;
using UnityEngine;

namespace huqiang.HotUIModel
{
    public class UIPage : UIBase
    {
        class PageInfo
        {
            public Type Pagetype;
            public object DataContext;
            public Type PopType;
            public object PopData;
        }
        static Stack<PageInfo> pages = new Stack<PageInfo>();
        public static Type typePop = typeof(PopWindow);
        public static void ClearStack()
        {
            pages.Clear();
        }
        public static UIElement UIRoot;
        public static void Initial(UIElement Canvas)
        {
            var page = new UIElement();
            page.name = "Page";
            UIRoot = page;
            page.SetParent(Canvas);
            UIRoot.localPosition = Vector3.zero;
            UIRoot.localScale = Vector3.one;
            UIRoot.localRotation = Quaternion.identity;
        }
        public static UIPage CurrentPage { get; private set; }
        public static T LoadPage<T>(object dat = null) where T : UIPage, new()
        {
            var p = CurrentPage as T;
            if (p!=null)
            {
                CurrentPage.ChangeLanguage();
                CurrentPage.Show(dat);
                return p;
            }
            if (HCanvas.CurrentCanvas != null)//释放当前页面所有事件
                HCanvas.CurrentCanvas.ClearAllAction();
            if (CurrentPage != null)
            {
                CurrentPage.Save();
                CurrentPage.Dispose();
            }
            var t = new T();
            CurrentPage = t;
            t.Initial(UIRoot, dat);
            t.ChangeLanguage();
            t.ReSize();
            return t;
        }
        public static UIPage LoadPage(Type type, object dat = null)
        {
            if (typeof(UIPage).IsAssignableFrom(type))
            {
                if (CurrentPage != null)
                    if (CurrentPage.GetType() == type)
                    {
                        CurrentPage.ChangeLanguage();
                        CurrentPage.Show(dat);
                        return CurrentPage;
                    }
                if (HCanvas.CurrentCanvas != null)//释放当前页面所有事件
                    HCanvas.CurrentCanvas.ClearAllAction();
                if (CurrentPage != null)
                    CurrentPage.Dispose();
                var t = Activator.CreateInstance(type) as UIPage;
                CurrentPage = t;
                t.Initial(UIRoot, dat);
                t.ChangeLanguage();
                t.ReSize();
                t.Recovery();
                return t;
            }
            return null;
        }
        public static void Back()
        {
            if (pages.Count > 0)
            {
                var page = pages.Pop();
                if (page != null)
                {
                    LoadPage(page.Pagetype, page.DataContext);
                    if (page.PopType != null)
                    {
                        CurrentPage.PopUpWindow(page.PopType, page.PopData);
                    }
                }
            }
        }
        public UIPage()
        {
            pops = new List<PopWindow>();
        }
        protected Type BackPage;
        protected Type BackPop;
        protected object BackData;
        public PopWindow currentPop { get; set; }
        public virtual void Initial(UIElement parent, object dat = null)
        {
            Parent = parent;
            DataContext = dat;
        }
        public virtual void Initial(UIElement parent, object dat = null, Type back = null, Type pop = null, object backData = null)
        {
            Initial(parent, dat);
            BackPage = back;
            BackPop = pop;
            BackData = backData;
        }
        public virtual void Show(object dat = null)
        {
        }
        public override void ReSize() 
        {
            if (UIRoot != null)
                if(HCanvas.CurrentCanvas!=null)
                UIRoot.SizeDelta = HCanvas.CurrentCanvas.SizeDelta;
            base.ReSize(); 
            if (currentPop != null) 
                currentPop.ReSize();
        }
        public override void Dispose()
        {
            if (pops != null)
                for (int i = 0; i < pops.Count; i++)
                    pops[i].Dispose();
            pops.Clear();
            currentPop = null;
            base.Dispose();
        }
        public void HidePopWindow()
        {
            if (currentPop != null)
            {
                currentPop.Hide();
            }
            currentPop = null;
        }
        List<PopWindow> pops;
        protected T ShowPopWindow<T>(object obj = null, UIElement parent = null) where T : PopWindow, new()
        {
            if (currentPop != null)
            {
                currentPop.Hide();
                currentPop = null;
            }
            for (int i = 0; i < pops.Count; i++)
                if (pops[i] is T)
                {
                    currentPop = pops[i];
                    pops[i].Show(obj);
                    if (pops[i].CurLan != LanName)
                        pops[i].ChangeLanguage();
                    return pops[i] as T;
                }
            var t = new T();
            pops.Add(t);
            currentPop = t;
            if (parent == null)
                t.Initial(Parent, this, obj);
            else t.Initial(parent, this, obj);
            t.ChangeLanguage();
            t.ReSize();
            return t;
        }
        protected object ShowPopWindow(Type type, object obj = null, UIElement parent = null)
        {
            if (currentPop != null)
            { 
                currentPop.Hide(); 
                currentPop = null; 
            }
            for (int i = 0; i < pops.Count; i++)
                if (pops[i].GetType() == type)
                {
                    currentPop = pops[i];
                    pops[i].Show(obj);
                    if (pops[i].CurLan != LanName)
                        pops[i].ChangeLanguage();
                    return pops[i];
                }
            var t = Activator.CreateInstance(type) as PopWindow;
            pops.Add(t);
            currentPop = t;
            if (parent == null)
                t.Initial(Parent, this, obj);
            else t.Initial(parent, this, obj);
            t.ChangeLanguage();
            t.ReSize();
            return t;
        }
        public virtual T PopUpWindow<T>(object obj = null) where T : PopWindow, new()
        {
            return ShowPopWindow<T>(obj, null);
        }
        object PopUpWindow(Type type, object obj = null)
        {
            var pop = ShowPopWindow(type, obj, null) as PopWindow;
            pop.Recovery();
            return pop;
        }
        /// <summary>
        /// 释放掉当前未激活的弹窗
        /// </summary>
        public void ReleasePopWindow()
        {
            if (pops != null)
                for (int i = 0; i < pops.Count; i++)
                    if (pops[i] != currentPop)
                        pops[i].Dispose();
            pops.Clear();
            if (currentPop != null)
                pops.Add(currentPop);
        }
        public void ReleasePopWindow(PopWindow window)
        {
            pops.Remove(window);
            if (currentPop == window)
            {
                currentPop = null;
            }
            window.Dispose();
        }
        public void ReleasePopWindow<T>()
        {
            for (int i = 0; i < pops.Count; i++)
                if (pops[i] is T)
                {
                    pops[i].Dispose();
                    pops.RemoveAt(i);
                    break;
                }
            if (currentPop is T)
            {
                currentPop = null;
            }
        }
        public T GetPopWindow<T>() where T : PopWindow
        {
            for (int i = 0; i < pops.Count; i++)
                if (pops[i] is T)
                {
                    return pops[i] as T;
                }
            return null;
        }
        public override void Save()
        {
            if (pops != null)
                for (int i = 0; i < pops.Count; i++)
                    if (pops[i] != currentPop)
                        pops[i].Save();
            PageInfo page = new PageInfo();
            page.Pagetype = GetType();
            if (currentPop != null)
                if (currentPop.Main.activeSelf)
                {
                    page.PopType = currentPop.GetType();
                    page.PopData = currentPop.DataContext;
                }
            page.DataContext = DataContext;
            pages.Push(page);
        }
        public override void Update(float time)
        {
            if (currentPop != null)
                currentPop.Update(time);
        }
        public override bool ChangeLanguage()
        {
            if (currentPop != null)
                currentPop.ChangeLanguage();
            return base.ChangeLanguage();
        }
    }
}